---@class LuaUIMgr
LuaUIMgr = {
    ---@type LuaUIMgr.Stack[]
    uiCacheStackList = {},  --缓存
    ---@type LuaUIMgr.Stack[][]
    uiStackList = {},
    LAYERS = {"MainParent","MainTopParent","PopParent","PopTopParent","MsgParent","TopParent","BaseTopParent","GuideOrder","ConnectOrder"},
    --fullScreenFlagList = {},
    isInit = false
}
local this = LuaUIMgr
function LuaUIMgr.ShowUI(uiPath)
    local name = getUIName2(uiPath)
    local layer = this.GetPanelLayer(uiPath)
    local _list = this.uiStackList[layer]
    if not _list then
        local stack = this.GetPool()
        stack:Init(uiPath)
        this.uiStackList[stack.layer] = {stack}
        return
    end
    local count = #_list
    for k,stack in pairs(_list) do
        if stack.name == name then
            if count == k then
                return
            else
                table.remove(_list,k)
                table.insert(_list,stack)
                return
            end
        end
    end
    local stack = this.GetPool()
    stack:Init(uiPath)
    table.insert(_list,stack)
    if panelMgr:IsOpen(name) then
        this.UpdateFullScreen()
    end
end
function LuaUIMgr.DestroyUIByResPath(resPath)
    this.CloseUIByResPath(resPath)
end
function LuaUIMgr.CloseUIByResPath(resPath)
    local layer = this.GetPanelLayer2(resPath)
    local _list = this.uiStackList[layer]
    if not _list then return end
    for k,v in pairs(_list) do
        if v.resPath == resPath then
            local s = table.remove(_list,k)
            this.ReturnPool(s)
            if v.resPath ~= PanelResNames.UICover and v.resPath ~= PanelResNames.UIPlayVideo then
                this.ClearUILayer(v.name)
            end
            break
        end
    end
    this.UpdateFullScreen()
end
--[[function LuaUIMgr.CloseUI(uiPath)
    local layer = this.GetPanelLayer(uiPath)
    local _list = this.uiStackList[layer]
    if not _list then return end
    for k,v in pairs(_list) do
        if v.uiPath == uiPath then
            local s = table.remove(_list,k)
            this.ReturnPool(s)
            break
        end
    end
    this.UpdateFullScreen()
end]]

function LuaUIMgr.UpdateFullScreen()
    local find = false
    for k = #this.LAYERS,1,-1 do
        local layer = this.LAYERS[k]
        local _list = this.uiStackList[layer]
        if _list then
            for i = #_list,1,-1 do
                local ui = _list[i]
                local name = getUIName2(ui.resPath)
                if find then
                    this.HideUILayer(ui.uiPath, name)
                else
                    if PanelFullScreen[ui.uiPath] then
                        find = true
                    end
                    this.ShowUILayer(ui.uiPath, name)
                end
            end
        end
    end
end
function LuaUIMgr.setIgnoreHideUI(name)
    this.ignoreHideUIName = name;
end
function LuaUIMgr.HideUILayer(uiPath,name)
    --if this.fullScreenFlagList[name] then return end
    if this.ignoreHideUIName == name then return end
    local success = panelMgr:SetUILayerMask(name,LayerMask.NameToLayer("Arrow"))
    if not success then return end
    --this.fullScreenFlagList[name] = true
    FireEvent(Config.EventType.UILayerMaskChanged,uiPath,false)
end
function LuaUIMgr.ShowUILayer(uiPath,name)
    --if not this.fullScreenFlagList[name] then return end
    local success
    if uiPath == PanelNames.UIPlayVideo or uiPath == PanelNames.UICover then
        success = panelMgr:SetUILayerMask(name,LayerMask.NameToLayer("Video"))
    else
        success = panelMgr:SetUILayerMask(name,LayerMask.NameToLayer("UI"))
    end


    if not success then return end
    --this.fullScreenFlagList[name] = nil
    FireEvent(Config.EventType.UILayerMaskChanged,uiPath,true)
end
function LuaUIMgr.ClearUILayer(name)
    --if not this.fullScreenFlagList[name] then return end
    panelMgr:SetUILayerMask(name,LayerMask.NameToLayer("UI"))
    --this.fullScreenFlagList[name] = nil
end
---@param obj GComponent
function LuaUIMgr.ObjectLoadFinished(obj)
    this.UpdateFullScreen()
end
---@param stack LuaUIMgr.Stack
function LuaUIMgr.AddStack(stack)
    local _list = this.uiStackList[stack.layer]
    if not _list then
        this.uiStackList[stack.layer] = {stack}
    else
        table.insert(_list,stack)
    end
end
---@return LuaUIMgr.Stack
function LuaUIMgr.GetPool()
    if #this.uiCacheStackList > 0 then
        return table.remove(this.uiCacheStackList)
    end
    local stack = this.Stack.New()
    return stack
end
---@param stack LuaUIMgr.Stack
function LuaUIMgr.ReturnPool(stack)
    if not stack then return end
    table.insert(this.uiCacheStackList,stack)
end
function LuaUIMgr.GetPanelLayer(uiPath)
    local name = getUIName2(uiPath)
    local resPath = PanelResNames[name]
    if resPath then
        name = getUIName2(resPath)
    end
    return PanelLayers[name] or this.LAYERS[1]
end
function LuaUIMgr.GetPanelLayer2(uiResPath)
    local name = getUIName2(uiResPath)
    return PanelLayers[name] or this.LAYERS[1]
end
LuaUIMgr.Stack = Class("")
---@class LuaUIMgr.Stack
local Stack = LuaUIMgr.Stack
function Stack:ctor()
    self.uiPath = ""
    self.layer = ""
    self.name = ""
    self.resPath = ""
end
function Stack:Init(uiPath)
    self.name = getUIName2(uiPath)
    self.uiPath = uiPath
    self.layer = this.GetPanelLayer(uiPath)
    self.resPath = PanelResNames[self.name] or self.uiPath

end


